/*     */ package com.cr.common.utils.http;
/*     */ 
/*     */ import java.io.BufferedReader;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.InputStreamReader;
/*     */ import java.io.PrintWriter;
/*     */ import java.net.ConnectException;
/*     */ import java.net.SocketTimeoutException;
/*     */ import java.net.URL;
/*     */ import java.net.URLConnection;
/*     */ import java.security.SecureRandom;
/*     */ import java.security.cert.X509Certificate;
/*     */ import javax.net.ssl.HostnameVerifier;
/*     */ import javax.net.ssl.HttpsURLConnection;
/*     */ import javax.net.ssl.SSLContext;
/*     */ import javax.net.ssl.SSLSession;
/*     */ import javax.net.ssl.TrustManager;
/*     */ import javax.net.ssl.X509TrustManager;
/*     */ import org.slf4j.Logger;
/*     */ import org.slf4j.LoggerFactory;
/*     */ 
/*     */ public class HttpUtils {
/*  30 */   private static final Logger log = LoggerFactory.getLogger(HttpUtils.class);
/*     */   
/*     */   public static String sendGet(String url, String param) {
/*  41 */     return sendGet(url, param, "UTF-8");
/*     */   }
/*     */   
/*     */   public static String sendGet(String url, String param, String contentType) {
/*  54 */     StringBuilder result = new StringBuilder();
/*  55 */     BufferedReader in = null;
/*     */     try {
/*  58 */       String urlNameString = url + "?" + param;
/*  59 */       log.info("sendGet - {}", urlNameString);
/*  60 */       URL realUrl = new URL(urlNameString);
/*  61 */       URLConnection connection = realUrl.openConnection();
/*  62 */       connection.setRequestProperty("accept", "*/*");
/*  63 */       connection.setRequestProperty("connection", "Keep-Alive");
/*  64 */       connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
/*  65 */       connection.connect();
/*  66 */       in = new BufferedReader(new InputStreamReader(connection.getInputStream(), contentType));
/*     */       String line;
/*  68 */       while ((line = in.readLine()) != null)
/*  70 */         result.append(line); 
/*  72 */       log.info("recv - {}", result);
/*  74 */     } catch (ConnectException e) {
/*  76 */       log.error("调用HttpUtils.sendGet ConnectException, url=" + url + ",param=" + param, e);
/*  78 */     } catch (SocketTimeoutException e) {
/*  80 */       log.error("调用HttpUtils.sendGet SocketTimeoutException, url=" + url + ",param=" + param, e);
/*  82 */     } catch (IOException e) {
/*  84 */       log.error("调用HttpUtils.sendGet IOException, url=" + url + ",param=" + param, e);
/*  86 */     } catch (Exception e) {
/*  88 */       log.error("调用HttpsUtil.sendGet Exception, url=" + url + ",param=" + param, e);
/*     */     } finally {
/*     */       try {
/*  94 */         if (in != null)
/*  96 */           in.close(); 
/*  99 */       } catch (Exception ex) {
/* 101 */         log.error("调用in.close Exception, url=" + url + ",param=" + param, ex);
/*     */       } 
/*     */     } 
/* 104 */     return result.toString();
/*     */   }
/*     */   
/*     */   public static String sendPost(String url, String param) {
/* 116 */     PrintWriter out = null;
/* 117 */     BufferedReader in = null;
/* 118 */     StringBuilder result = new StringBuilder();
/*     */     try {
/* 121 */       String urlNameString = url;
/* 122 */       log.info("sendPost - {}", urlNameString);
/* 123 */       URL realUrl = new URL(urlNameString);
/* 124 */       URLConnection conn = realUrl.openConnection();
/* 125 */       conn.setRequestProperty("accept", "*/*");
/* 126 */       conn.setRequestProperty("connection", "Keep-Alive");
/* 127 */       conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
/* 128 */       conn.setRequestProperty("Accept-Charset", "utf-8");
/* 129 */       conn.setRequestProperty("contentType", "utf-8");
/* 130 */       conn.setDoOutput(true);
/* 131 */       conn.setDoInput(true);
/* 132 */       out = new PrintWriter(conn.getOutputStream());
/* 133 */       out.print(param);
/* 134 */       out.flush();
/* 135 */       in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
/*     */       String line;
/* 137 */       while ((line = in.readLine()) != null)
/* 139 */         result.append(line); 
/* 141 */       log.info("recv - {}", result);
/* 143 */     } catch (ConnectException e) {
/* 145 */       log.error("调用HttpUtils.sendPost ConnectException, url=" + url + ",param=" + param, e);
/* 147 */     } catch (SocketTimeoutException e) {
/* 149 */       log.error("调用HttpUtils.sendPost SocketTimeoutException, url=" + url + ",param=" + param, e);
/* 151 */     } catch (IOException e) {
/* 153 */       log.error("调用HttpUtils.sendPost IOException, url=" + url + ",param=" + param, e);
/* 155 */     } catch (Exception e) {
/* 157 */       log.error("调用HttpsUtil.sendPost Exception, url=" + url + ",param=" + param, e);
/*     */     } finally {
/*     */       try {
/* 163 */         if (out != null)
/* 165 */           out.close(); 
/* 167 */         if (in != null)
/* 169 */           in.close(); 
/* 172 */       } catch (IOException ex) {
/* 174 */         log.error("调用in.close Exception, url=" + url + ",param=" + param, ex);
/*     */       } 
/*     */     } 
/* 177 */     return result.toString();
/*     */   }
/*     */   
/*     */   public static String sendSSLPost(String url, String param) {
/* 182 */     StringBuilder result = new StringBuilder();
/* 183 */     String urlNameString = url + "?" + param;
/*     */     try {
/* 186 */       log.info("sendSSLPost - {}", urlNameString);
/* 187 */       SSLContext sc = SSLContext.getInstance("SSL");
/* 188 */       sc.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new SecureRandom());
/* 189 */       URL console = new URL(urlNameString);
/* 190 */       HttpsURLConnection conn = (HttpsURLConnection)console.openConnection();
/* 191 */       conn.setRequestProperty("accept", "*/*");
/* 192 */       conn.setRequestProperty("connection", "Keep-Alive");
/* 193 */       conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
/* 194 */       conn.setRequestProperty("Accept-Charset", "utf-8");
/* 195 */       conn.setRequestProperty("contentType", "utf-8");
/* 196 */       conn.setDoOutput(true);
/* 197 */       conn.setDoInput(true);
/* 199 */       conn.setSSLSocketFactory(sc.getSocketFactory());
/* 200 */       conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
/* 201 */       conn.connect();
/* 202 */       InputStream is = conn.getInputStream();
/* 203 */       BufferedReader br = new BufferedReader(new InputStreamReader(is));
/* 204 */       String ret = "";
/* 205 */       while ((ret = br.readLine()) != null) {
/* 207 */         if (ret != null && !ret.trim().equals(""))
/* 209 */           result.append(new String(ret.getBytes("ISO-8859-1"), "utf-8")); 
/*     */       } 
/* 212 */       log.info("recv - {}", result);
/* 213 */       conn.disconnect();
/* 214 */       br.close();
/* 216 */     } catch (ConnectException e) {
/* 218 */       log.error("调用HttpUtils.sendSSLPost ConnectException, url=" + url + ",param=" + param, e);
/* 220 */     } catch (SocketTimeoutException e) {
/* 222 */       log.error("调用HttpUtils.sendSSLPost SocketTimeoutException, url=" + url + ",param=" + param, e);
/* 224 */     } catch (IOException e) {
/* 226 */       log.error("调用HttpUtils.sendSSLPost IOException, url=" + url + ",param=" + param, e);
/* 228 */     } catch (Exception e) {
/* 230 */       log.error("调用HttpsUtil.sendSSLPost Exception, url=" + url + ",param=" + param, e);
/*     */     } 
/* 232 */     return result.toString();
/*     */   }
/*     */   
/*     */   private static class TrustAnyTrustManager implements X509TrustManager {
/*     */     private TrustAnyTrustManager() {}
/*     */     
/*     */     public void checkClientTrusted(X509Certificate[] chain, String authType) {}
/*     */     
/*     */     public void checkServerTrusted(X509Certificate[] chain, String authType) {}
/*     */     
/*     */     public X509Certificate[] getAcceptedIssuers() {
/* 250 */       return new X509Certificate[0];
/*     */     }
/*     */   }
/*     */   
/*     */   private static class TrustAnyHostnameVerifier implements HostnameVerifier {
/*     */     private TrustAnyHostnameVerifier() {}
/*     */     
/*     */     public boolean verify(String hostname, SSLSession session) {
/* 259 */       return true;
/*     */     }
/*     */   }
/*     */ }


/* Location:              D:\tmp\creditorRights-admin\BOOT-INF\lib\creditorRights-common-4.7.0.jar!\com\cr\commo\\utils\http\HttpUtils.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */